from django.db import models

from foodmarket.settings import MEDIA_HOST


class Category(models.Model):
    parent_category = models.ForeignKey(
        'Category',
        null=True,
        blank=True,
        verbose_name=u'上级分类',
        on_delete=models.CASCADE,
        related_name='sub_categories'
    )
    name = models.CharField(max_length=30, verbose_name=u'分类')
    image = models.ImageField(upload_to='img', max_length=1000, null=True, blank=True, verbose_name=u'图片')
    description = models.TextField(null=True, blank=True, verbose_name=u'详细说明')

    def __str__(self):
        return self.name

    @property
    def image_url(self):
        try:
            return MEDIA_HOST + self.image.url
        except ValueError:
            return ''

    class Meta:
        verbose_name = u'分类'
        verbose_name_plural = u'分类'
